package com.dnjn.happiness.emo.webapi.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;

import java.util.Date;


public class JwtUtil {

    //5分钟过期
    public static final long JWT_EXPIRATION = (long) 5 * 60000;

    /**
     * iat 签发日期 withIssuedAt
     * nbf 生效时间 withNotBefore
     * exp 过期时间 withExpiresAt
     * sub 主题
     * iss 签发人
     * aud 接收方
     */
    public static String createJwtToken(String jwtKey, String jwtSecret) {
        String token = null;
        try {
            Date date = new Date();
            token = JWT.create()
                    .withIssuer(jwtKey)
                    .withIssuedAt(date)
                    .withNotBefore(new Date(date.getTime() - JWT_EXPIRATION))
                    .withExpiresAt(new Date(date.getTime() + JWT_EXPIRATION))
                    .sign(Algorithm.HMAC256(jwtSecret));
        } catch (JWTCreationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public static void main(String[] args) {
        //生成Token值

        String generateToken = JwtUtil.createJwtToken("MM-XKQ0534W", "6f290feZ-2302-46ca-arfa-177fs060e758");
        System.out.println(generateToken);
    }
}

